package com.fenghongzhang.module_main.splash

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.View
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
import com.alibaba.android.arouter.launcher.ARouter
import com.fenghongzhang.lib_core.base.BaseActivity
import com.fenghongzhang.lib_core.ui.CommonWebActivity
import com.fenghongzhang.lib_core.utils.Router
import com.fenghongzhang.lib_core.utils.getVersionCode
import com.fenghongzhang.module_main.BuildConfig

import com.fenghongzhang.module_main.R


import com.fenghongzhang.module_main.databinding.ActivitySplashBinding
import com.fenghongzhang.module_main.dialog.MessageNoticeDialog
import com.gyf.immersionbar.ktx.immersionBar
import com.jczy.cyclone.mmkv.AppMMKV
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class SplashActivity : BaseActivity<SplashViewModel, ActivitySplashBinding>() {


    companion object {
        //最大延迟
        private const val SPLASH_MAX_DELAY: Long = 1_500
    }
    //初始化结束后操作
    private var actionAfterInitFinished: (() -> Unit)? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
    }

    override val mViewModel: SplashViewModel by viewModels()
    override val mLayoutResId: Int = R.layout.activity_splash

    private val mHandler by lazy {
        object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                if (mViewModel.mAppStartAdInfo?.imageUrl.isNullOrEmpty()) {
                    if (AppMMKV.needGuide) {
                        toGuide()
                    } else {
                        toMain()
                    }
                } else {
                    toAD()
                }
            }
        }
    }

    private fun toMain() {
        ARouter.getInstance()
            .build(Router.Ui.MainActivity)
            //  .withTransition(R.anim.right_in, R.anim.left_out)
            .navigation(this)
        actionAfterInitFinished?.invoke()
        ActivityCompat.finishAffinity(this)
        if (BuildConfig.DEBUG) {
//            ClubHomeMgrActivity.jump("1572366655316152321")
//            ARouterUtils.jump(Router.Ui.MyCreatedClubActivity)
//            ARouterUtils.jump(Router.Ui.TestClubHomeActivity)
        }
    }

    private fun toGuide() {
        ARouter.getInstance()
            .build(Router.Ui.GuideActivity)
            //   .withTransition(R.anim.right_in, R.anim.left_out)
            .navigation(this)
        actionAfterInitFinished?.invoke()
        ActivityCompat.finishAffinity(this)
    }


    private fun toAD() {
//        AdActivity.jump(mViewModel.mAppStartAdInfo)
//        actionAfterInitFinished?.invoke()
//        ActivityCompat.finishAffinity(this)
    }

    override fun initView() {
        mHandler.sendMessage(Message.obtain())

    }
    override fun initListener() {

    }

    override fun initData() {
     //   if (!BuildConfig.DEBUG) {
            mViewModel.cMallAdInfo("skl_app_start")
      //  }

        initStatusBar()
        if (!AppMMKV.splashAgreement) {//get
            //取个颜色
           // val colorMain = getAttrValue(this, com.fenghongzhang.lib_core.R.attr.colorTheme)
            val userAgreement = "《用户协议》"
            val privacyPolicy = "《隐私条款》"
            val content =
                "宗申骑士俱乐部非常尊重用户个人的信息保护，我们依据最新的监管要求特向您说明如下：\n1.在使用 APP过程中，我们有可能会收集、使用设备标识信息用于推送车辆提醒消息。\n2.我们会一直采取严格的安全技术保护您的个人信息安全。\n3.未经您的同意，我们不会以协议约定之外的形式和渠道获取、共享或使用您的个人信息。\n您可通过阅读完整的${userAgreement}和${privacyPolicy}来了解详细信息。请您充分阅读并理解以上协议，点击同意按钮，即表示您已同意上述协议及以上约定。"
            val spannableStringBuilder = SpannableStringBuilder(content)
            val userAgreementStart = content.indexOf(userAgreement)
            val userAgreementEnd = userAgreementStart + userAgreement.length
            val privacyPolicyStart = content.indexOf(privacyPolicy)
            val privacyPolicyEnd = privacyPolicyStart + privacyPolicy.length

            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
                    CommonWebActivity.jumpUserAgreement()
                }

                override fun updateDrawState(ds: TextPaint) {
                  //  ds.color = ContextCompat.getColor(this@SplashActivity, colorMain)
                    ds.isUnderlineText = false
                }

            }, userAgreementStart, userAgreementEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)

            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
                    CommonWebActivity.jumpPrivacyAgreement()
                }

                override fun updateDrawState(ds: TextPaint) {
                  //  ds.color = ContextCompat.getColor(this@SplashActivity, colorMain)
                    ds.isUnderlineText = false
                }

            }, privacyPolicyStart, privacyPolicyEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            MessageNoticeDialog(
                title = "温馨提示",
                message = spannableStringBuilder,
                cancelText = "不同意",
                confirmText = "同意",
                alignment = Gravity.START,
                onConfirmClick = {
                    //同意隐私政策
                    AppMMKV.splashAgreement = true
                    //记录当前版本号
                    AppMMKV.lastVersionCode = getVersionCode(this) //1000
                    mHandler.sendEmptyMessageDelayed(2, SPLASH_MAX_DELAY)
                },
                onCancelClick = { finish() }
            ).show(supportFragmentManager)
        }
    }

    override fun initStatusBar() {
        immersionBar {
            this.statusBarDarkFont(true)
        }
    }

}